﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MODULE_DANHMUC
{
    public partial class frm_danhmucnhanvien : Form
    {
        int currenrowindex;
        danhmucDataContext context = cls_danhmuc.getcontext();
        public frm_danhmucnhanvien()
        {
            InitializeComponent();
        }

       
             private void getdata()
        {
            
            List<P_getmanhanvienResult> manvs = context.P_getmanhanvien().ToList() ;
            if (manvs.Count > 0)
            {
                this.txtmanv.Text = manvs.ElementAt(0).manv.ToString();
                this.txtusername.Text = manvs.ElementAt(0).manv.ToString();
                this.txtmatkhau.Text = manvs.ElementAt(0).manv.ToString();
            }

                 //Lay du lieu cho luoi

            this.grdata.Rows.Clear();
            
            // this.grdata.DataSource = context.DM_hanghoas;
            IEnumerable<DM_nhanvien> list = context.DM_nhanviens.AsEnumerable();
            for (int i = 0; i < list.Count(); i++)
            {
               DM_nhanvien item = list.ElementAt(i);
               this.grdata.Rows.Add();
                
               this.grdata.Rows[i].Cells["manv"].Value = item.manv;
               this.grdata.Rows[i].Cells["tennv"].Value = item.hoten;
                
               ///lam them cho nay
               IEnumerable<DM_chucvu> l = from c in context.DM_chucvus where c.idchucvu.Equals(item.idchucvu) select c;
                DM_chucvu cv=new DM_chucvu();
                if (l.Count() > 0)
               {
                   cv= l.ElementAt(0);
               }
                this.grdata.Rows[i].Cells["chucvu"].Value = cv.tenchucvu;
                this.grdata.Rows[i].Cells["dienthoai"].Value = item.dienthoai;


           }
           
        }
       
        private void frm_danhmucnhanvien_Load(object sender, EventArgs e)
        {
            this.cbchucvu.DataSource = context.DM_chucvus;
            this.cbchucvu.DisplayMember = "tenchucvu";
            this.cbchucvu.ValueMember = "idchucvu";
            this.txttenvn.Text = "";
            this.txtsodienthoai.Text = "";
            getdata();
           
            

        }

        private void btSave_Click(object sender, EventArgs e)
        {
            DM_nhanvien nv = new DM_nhanvien();
            nv.manv = this.txtmanv.Text;
            nv.dienthoai = this.txtsodienthoai.Text;
            nv.hoten = this.txttenvn.Text;
            nv.idchucvu = decimal.Parse(this.cbchucvu.SelectedValue.ToString());
            nv.username = this.txtusername.Text;
            string password = mycryptography.cls_mycryptography.EncryptString("daiviet",this.txtmatkhau.Text);
            nv.password = password;
            if (this.chkco.Checked == true)
            {
                nv.trangthai = true;
            }
            if (this.chkkhong.Checked == true)
            {
                nv.trangthai = false;
            }
            nv.loaiuser = 1;
            try
            {

                IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.username.Equals(nv.username) select c;
                if (nhanviens.Count() > 0)//trung ten dang nhap
                {

                    IEnumerable<DM_nhanvien> nhanviens1 = from c in context.DM_nhanviens where c.manv.Equals(nv.manv) select c;
                    if (nhanviens1.Count() > 0)//cap nhat thong tin cua nhan vien cu
                    {
                        foreach (DM_nhanvien nhanvien in nhanviens1)
                        {
                            nhanvien.hoten = nv.hoten;
                            nhanvien.idchucvu = nv.idchucvu;
                            nhanvien.trangthai = nv.trangthai;
                            nhanvien.dienthoai = nv.dienthoai;
                        }
                        context.SubmitChanges();
                        MessageBox.Show("Cập nhật thành công.");

                    }
                    else//tao moi bi trung
                    {
                        MessageBox.Show("Tên đăng nhập đã tồn tại trên hệ thống.Nhập lại tên khác");
                    }
                }
                else
                {


                     nhanviens = from c in context.DM_nhanviens where c.manv.Equals(nv.manv) where c.username.Equals(nv.username) select c;

                    if (nhanviens.Count() > 0)
                    {
                        foreach (DM_nhanvien n in nhanviens)
                        {

                            n.dienthoai = nv.dienthoai;
                            n.hoten = nv.hoten;
                            n.idchucvu = nv.idchucvu;
                            n.username = nv.username;
                            n.trangthai = nv.trangthai;
                            n.loaiuser = 1;
                        }


                    }
                    else
                    {
                        context.DM_nhanviens.InsertOnSubmit(nv);
                    }

                    context.SubmitChanges();
                    MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không lưu được.Lỗi: "+ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
            getdata();

        }

        private void getrowdata()
        {
            if (currenrowindex < grdata.Rows.Count)
            {
                this.txtmatkhau.ReadOnly = true;
                this.txtmatkhau.ReadOnly = true;
                string manv = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
                IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.manv.Equals(manv) select c;
                DM_nhanvien nhanvien = nhanviens.ElementAt(0);
                this.txtmanv.Text = nhanvien.manv;
                this.txttenvn.Text = nhanvien.hoten;
                this.cbchucvu.SelectedValue = nhanvien.idchucvu;
                this.txtsodienthoai.Text = nhanvien.dienthoai;
                this.txtusername.Text = nhanvien.username;
                this.txtmatkhau.Text = "";
                if (nhanvien.trangthai == null)
                {
                    this.chkco.Checked = false;
                    this.chkkhong.Checked = false;
                }
                if (nhanvien.trangthai == true)
                {
                    this.chkco.Checked = true;
                    this.chkkhong.Checked = false;
                }
                if (nhanvien.trangthai == false)
                {
                    this.chkco.Checked = false;
                    this.chkkhong.Checked = true;
                }

            }

        }
        private void grdata_Click(object sender, EventArgs e)
        {
            currenrowindex = this.grdata.CurrentRow.Index;
            this.grdata.Rows[currenrowindex].Selected = true;
            getrowdata();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
            if (currenrowindex < this.grdata.Rows.Count-1)
            {
                currenrowindex = currenrowindex + 1;
            }

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;

            currenrowindex = this.grdata.Rows.Count - 1;


            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;
            if (currenrowindex > 0)
            {
                currenrowindex = currenrowindex - 1;
            }

            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.grdata.Rows[currenrowindex].Selected = false;

            currenrowindex = 0;


            this.grdata.Rows[currenrowindex].Selected = true;

            getrowdata();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            try
            {
               // DM_hanghoa hh = new DM_hanghoa();
                string manv = this.txtmanv.Text;
                //hh.mahang = this.grdata.Rows[currenrowindex].Cells[0].Value == null ? "" : this.grdata.Rows[currenrowindex].Cells[0].Value.ToString();
                //hh.tenhang = this.grdata.Rows[currenrowindex].Cells[1].Value == null ? "" : this.grdata.Rows[currenrowindex].Cells[1].Value.ToString();
                IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.manv.Equals(manv) select c;



                // MessageBox.Show("Bạn chắc chắn muốn xóa?", "Xác nhận",MessageBoxButtons.YesNo);





                foreach (DM_nhanvien nhanvien in nhanviens)
                {
                    context.DM_nhanviens.DeleteOnSubmit(nhanvien);
                }
                context.SubmitChanges();

                MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không xóa được.Mã nhân viên đang được sử dụng");
                //MessageBox.Show("Không xóa được.Lỗi: "+ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
            getdata();
        }

        private void chkco_Click(object sender, EventArgs e)
        {
            if (this.chkco.Checked == true)
            {
                this.chkco.Checked = false;
                chkkhong.Checked = true;
            }
            if (this.chkco.Checked == false)
            {
                this.chkco.Checked = true;
                this.chkkhong.Checked = false;
            }
        }

        private void chkkhong_Click(object sender, EventArgs e)
        {
            if (this.chkkhong.Checked == true)
            {
                this.chkkhong.Checked = false;
                chkco.Checked = true;
            }
            if (this.chkkhong.Checked == false)
            {
                this.chkkhong.Checked = true;
                this.chkco.Checked = false;
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.txttenvn.Text = "";
                this.txtsodienthoai.Text = "";
                this.txtusername.ReadOnly = false;
                this.txtmatkhau.ReadOnly = false;
                List<P_getmanhanvienResult> manvs = context.P_getmanhanvien().ToList();
                if (manvs.Count > 0)
                {
                    this.txtmanv.Text = manvs.ElementAt(0).manv.ToString();
                    this.txtusername.Text = manvs.ElementAt(0).manv.ToString();
                    this.txtmatkhau.Text = manvs.ElementAt(0).manv.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thêm được!Lỗi: " + ex.Message);
            }

        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            this.tabControl1.SelectedIndex = 1;
            string keyword = txtkeyword.Text;
            this.grdata.Rows.Clear();

            // this.grdata.DataSource = context.DM_hanghoas;
            IEnumerable<DM_nhanvien> list = from c in context.DM_nhanviens where c.hoten.Contains(keyword)select c;
            for (int i = 0; i < list.Count(); i++)
            {
                DM_nhanvien item = list.ElementAt(i);
                this.grdata.Rows.Add();

                this.grdata.Rows[i].Cells["manv"].Value = item.manv;
                this.grdata.Rows[i].Cells["tennv"].Value = item.hoten;

                ///lam them cho nay
                IEnumerable<DM_chucvu> l = from c in context.DM_chucvus where c.idchucvu.Equals(item.idchucvu) select c;
                DM_chucvu cv = new DM_chucvu();
                if (l.Count() > 0)
                {
                    cv = l.ElementAt(0);
                }
                this.grdata.Rows[i].Cells["chucvu"].Value = cv.tenchucvu;
                this.grdata.Rows[i].Cells["dienthoai"].Value = item.dienthoai;


            }


        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}
